//
//  UserInfoChangeCell.m
//  CBT
//
//  Created by Mr.Zhang on 2018/11/22.
//  Copyright © 2018 Mr.Zhang. All rights reserved.
//

#import "UserInfoChangeCell.h"

@interface UserInfoChangeCell()

@property (nonatomic, strong) UIImageView *iconImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIImageView *rightImageView;
@property (nonatomic, strong) UIImageView *userIconImageView;

@end

@implementation UserInfoChangeCell

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)changeImageViewImage:(UIImage *)image
{
    self.userIconImageView.image = image;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor whiteColor];
        [self initSubViews];
    }
    return self;
}

-(void)setUserInfoDic:(NSDictionary *)userInfoDic
{
    _titleLabel.text = kLocalizedString(userInfoDic[@"title"], nil);
    _iconImageView.image = [UIImage imageNamed:userInfoDic[@"img"]];
}

- (void)initSubViews
{
    _iconImageView = [[UIImageView alloc]init];
    [self.contentView addSubview:_iconImageView];
    
    _titleLabel = [[UILabel alloc]init];
    _titleLabel.font = [UIFont systemFontOfSize:15];
    _titleLabel.textColor = UIColorFromRGB(0x333333);
    [self.contentView addSubview:_titleLabel];
    
    _userIconImageView = [[UIImageView alloc]init];
    _userIconImageView.contentMode = UIViewContentModeScaleAspectFill;
    _userIconImageView.layer.cornerRadius = 21;
    _userIconImageView.layer.masksToBounds = YES;
    
    if ([[CBTImageTools getUserImageNameWithUserID:[CBTLocalUserManager sharedInstance].userId] isEqualToString:@"-1"]) {
        [_userIconImageView lf_setImageURL:[NSString stringWithFormat:@"%@/%@",kBaseURL_PICSTATIC,[CBTLocalUserManager sharedInstance].headImage]];
    }
    else {
        _userIconImageView.image = [UIImage imageWithContentsOfFile:[CBTImageTools getUserImageNameWithUserID:[CBTLocalUserManager sharedInstance].userId]];
    }

    _userIconImageView.backgroundColor = UIColorFromRGB(0xdbdbdb);
    [self.contentView addSubview:_userIconImageView];
    
    _rightImageView = [[UIImageView alloc]init];
    _rightImageView.image = [UIImage imageNamed:@"mine_next"];
    [self.contentView addSubview:_rightImageView];
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    __weak typeof(self) weakSelf = self;
    [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_offset(20);
        make.width.height.mas_offset(19);
    }];
    
    [_userIconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(weakSelf.rightImageView).offset(-15);
        make.top.mas_offset(9);
        make.width.height.mas_offset(42);
    }];
    
    [_rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(weakSelf.titleLabel);
        make.width.mas_offset(5);
        make.height.mas_offset(9);
    }];
    
    _userIconImageView.hidden = self.indexRow == 1;
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.iconImageView.mas_right).offset(12);
        make.top.bottom.mas_offset(0);
        if (weakSelf.indexRow == 0) {
        make.right.mas_equalTo(weakSelf.userIconImageView.mas_left).offset(-12);
        }
        else {
        make.right.mas_equalTo(weakSelf.rightImageView.mas_left).offset(-12);
        }
    }];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end
